<template>
  <el-row>

    <CreateDialog @refresh="refresh" :item="item" :visible.sync="addProductModalVisible" :id="productId"></CreateDialog>

    <product-detail url="/ms-warehouse-order/basicData/findManufacturersProductInfo"
                    :visible.sync="productDetailModalVisible" :id="productId"></product-detail>

    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="productNo">
                <el-input v-model="search.productNo" placeholder="商品编号"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="productName">
                <el-input v-model="search.productName" placeholder="商品名称"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="productCodeArr">
                <product-select ref="productSelect" v-model="search.productCodeArr"></product-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="storageTypeCode">
                <el-select v-model="search.storageTypeCode" placeholder="材料类型">
                  <el-option v-for="item in storageTypeCodeArr" :key="item.value" :label="item.text"
                             :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain @click.prevent="searchFunc(search)"><span>搜索</span>
                    </el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>

    <el-col :span="24">
      <basic-table
        ref="dataTable"
        url="/ms-warehouse-order/basicData/findManufacturersProductList"
        :columns="columns"
        :initParams="{manufacturersId: this.id}"
      >
        <template slot="column">
          <el-table-column fixed="right" align="center" width="120px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="addProduct(scope.row)"
              >
                修改商品
              </el-button>
            </template>
          </el-table-column>
        </template>
        <template slot="buttons">
          <el-button type="primary" @click.prevent="showCreateDialog">新增厂家商品</el-button>
        </template>
      </basic-table>
    </el-col>
  </el-row>
</template>
<script>
import {copy} from '@/utils'
import {urlPrefix} from '@/config/index'
import {unixTimestampFilter} from '@/filters'
import {getSubDictListOfParentCode} from '@/utils/selectOptions'
import CreateDialog from '@/views/oms/basicData/manufactorPro/CreateDialog'
import ProductDetail from '@/views/oms/basicData/modal/ProductDetail'

export default {
  name: 'basicDataManufactorProIndex',
  components: {
    CreateDialog,
    ProductDetail
  },
  data() {
    return {
      urlPrefix,
      tabIndex: 1,
      search: {
        productNo: '',
        productName: '',
        productCodeArr: [],
        storageTypeCode: ''
      },
      id: this.$route.params.id,
      productId: '',
      cargoOwnerId: '',
      row: {},
      addProductModalVisible: false,
      productDetailModalVisible: false,
      storageTypeCodeArr: [],
      subOrderFinish: '', // 一个消息订阅
      visible: false,
      resultVisible: false,
      isOpen: false,
      searchLoading: false,
      item: {manufacturersName: this.$route.query.manufacturersName, id: this.$route.params.id},
      columns: [
        {label: '厂家名称', data: 'manufacturersName'},
        {label: '商品编号', data: 'productNo'},
        {label: '商品型号', data: 'productOutNo'},
        {label: '商品名称', data: 'productName'},
        {
          label: '商品类别',
          data: 'bigClass',
          render(data, full) {
            return `${full.bigClass}/${full.middleClass}`
          }
        },
        {
          label: '总包件',
          data: 'totalPackageNum',
          render(data, full) {
            return data || '/' // 默认展示 【/】form 梁毅
          }
        },
        {
          label: '总体积(m³)',
          data: 'totalVolume',
          render(data, full) {
            return data || '/' // form 梁毅
          }
        },
        {
          label: '总重量(kg)',
          data: 'totalWeight',
          render(data, full) {
            return data || '/' // form 梁毅
          }
        },
        {
          label: '材料类型',
          data: 'storageType',
          render(data, full) {
            return data || '/' // form 梁毅
          }
        },
        {
          label: '规格',
          data: 'standard',
          render(data, full) {
            return data || '/' // form 梁毅
          }
        },
        {label: '操作人', data: 'founder'},
        {
          label: '添加时间',
          data: 'foundDate',
          render(data, full) {
            return unixTimestampFilter(data)
          }
        }
      ]
    }
  },
  created() {
    getSubDictListOfParentCode('STORAGETYPE').then(arr => {
      this.storageTypeCodeArr = arr
    })
  },
  methods: {
    searchFunc(search) {
      let data = copy(search, true)
      data.bigClassNo = data.productCodeArr[0] || ''
      data.middleClassNo = data.productCodeArr[1] || ''
      delete data.productCodeArr
      this.searchLoading = true
      this.$refs.dataTable.search(data).finally(_ => {
        this.searchLoading = false
      })
    },
    resetFields(searchForm) {
      this.$refs.searchForm.resetFields()
    },
    refresh() {
      this.searchFunc(this.search)
    },
    show(data) {
      this.productId = data.id
      this.productDetailModalVisible = true
    },
    // 编辑产品
    addProduct(data) {
      this.productId = data.id
      this.addProductModalVisible = true
    },
    // 新增产品
    showCreateDialog() {
      this.productId = ''
      this.addProductModalVisible = true
    }
  }
}
</script>
